NVM.TopSearch = (function () {
  var __core = NOVEM.Core;

  return {
    init: function (selector) {
      if (!__core.getOptions.search) {
        console.warn(
          "No API Key defined for Search! Please set an API Key in js/functions.js File!"
        );
        return true;
      }

      selector = __core.getSelector(selector, false);
      if (selector.length < 1) {
        return true;
      }

      const nvmMajors = [
        {
          name: "高速铁路客运服务专业",
          url: "https://www.zcu.edu.cn/info/1281/12271.htm",
        },
        {
          name: "建筑装饰工程技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12411.htm",
        },
        {
          name: "计算机应用技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12281.htm",
        },
        {
          name: "电子商务专业",
          url: "https://www.zcu.edu.cn/info/1281/12601.htm",
        },
        {
          name: "新能源汽车技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12611.htm",
        },
        {
          name: "数字媒体技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12301.htm",
        },
        {
          name: "大数据与会计专业",
          url: "https://www.zcu.edu.cn/info/1281/12631.htm",
        },
        {
          name: "大数据技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12471.htm",
        },
        {
          name: "建筑工程技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12291.htm",
        },
        {
          name: "艺术设计专业",
          url: "https://www.zcu.edu.cn/info/1281/12311.htm",
        },
        {
          name: "机电一体化技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12621.htm",
        },
        {
          name: "影视多媒体技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12401.htm",
        },
        {
          name: "电子商务专业（新媒体营销）",
          url: "https://www.zcu.edu.cn/info/1281/12511.htm",
        },
        {
          name: "环境艺术设计专业",
          url: "https://www.zcu.edu.cn/info/1281/12391.htm",
        },
        {
          name: "早期教育专业（师范）",
          url: "https://www.zcu.edu.cn/info/1281/12591.htm",
        },
        {
          name: "机械设计与制造专业",
          url: "https://www.zcu.edu.cn/info/1281/12651.htm",
        },
        {
          name: "学前教育专业",
          url: "https://www.zcu.edu.cn/info/1281/12481.htm",
        },
        {
          name: "旅游管理专业",
          url: "https://www.zcu.edu.cn/info/1281/12581.htm",
        },
        {
          name: "现代物流管理专业",
          url: "https://www.zcu.edu.cn/info/1281/12681.htm",
        },
        {
          name: "复合材料智能制造技术专业",
          url: "https://www.zcu.edu.cn/info/1281/17371.htm",
        },
        {
          name: "电气自动化技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12431.htm",
        },
        {
          name: "汽车制造与试验技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12661.htm",
        },
        {
          name: "工商企业管理专业",
          url: "https://www.zcu.edu.cn/info/1281/12671.htm",
        },
        {
          name: "空中乘务专业",
          url: "https://www.zcu.edu.cn/info/1281/12341.htm",
        },
        {
          name: "金融服务与管理专业",
          url: "https://www.zcu.edu.cn/info/1281/12641.htm",
        },
        {
          name: "道路与桥梁工程技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12371.htm",
        },
        {
          name: "酒店管理与数字化运营专业",
          url: "https://www.zcu.edu.cn/info/1281/12571.htm",
        },
        {
          name: "工业机器人技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12451.htm",
        },
        {
          name: "机械制造及自动化专业",
          url: "https://www.zcu.edu.cn/info/1281/12561.htm",
        },
        {
          name: "工程造价专业",
          url: "https://www.zcu.edu.cn/info/1281/12331.htm",
        },
        {
          name: "应用英语专业",
          url: "https://www.zcu.edu.cn/info/1281/12531.htm",
        },
        {
          name: "软件技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12321.htm",
        },
        {
          name: "数字媒体技术专业（三维动画设计）",
          url: "https://www.zcu.edu.cn/info/1281/12421.htm",
        },
        {
          name: "新型建筑材料技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12501.htm",
        },
        {
          name: "人工智能技术应用专业",
          url: "https://www.zcu.edu.cn/info/1281/12521.htm",
        },
        {
          name: "视觉传达设计专业",
          url: "https://www.zcu.edu.cn/info/1281/12351.htm",
        },
        {
          name: "材料工程技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12441.htm",
        },
        {
          name: "市场营销专业",
          url: "https://www.zcu.edu.cn/info/1281/12551.htm",
        },
        {
          name: "动漫制作技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12361.htm",
        },
        {
          name: "汽车检测与维修技术专业",
          url: "https://www.zcu.edu.cn/info/1281/12541.htm",
        },
        {
          name: "音乐表演专业",
          url: "https://www.zcu.edu.cn/info/1281/12491.htm",
        },
      ];

      // 专业搜索 参考https://www.nottingham.edu.cn/cn/index.aspx
      // 待优化专业名称模糊搜索 https://github.com/krisk/fuse
      // 数据源https://www.zcu.edu.cn/s?ac=w传值keyword
      const nvmSearch = document.querySelector(".search-wrapper");
      const searchKey = document.querySelector(".search-input");
      const searchBtn = document.querySelector(".search-btn");
      const searchClear = document.querySelector(".search-clear");
      const searchResult = document.querySelector(".search-result");
      let searchKW = "";
      const searchResultToggle = () => {
        let nvmSearchResult = (searchResult.innerHTML = "");
        for (const nvmMajor of nvmMajors) {
          if (nvmMajor.name.indexOf(searchKW) !== -1) {
            nvmSearchResult += "<li><a href='" + nvmMajor.url + "' target='_blank' title='" + nvmMajor.name + "'>" + nvmMajor.name + "</a></li>";
          }
        };
        if (nvmSearchResult) {
          searchResult.innerHTML = nvmSearchResult;
        } else {
          // searchResult.innerHTML = "<li>未找到您要搜索的内容</li>";
          searchResult.innerHTML =
            "<li><a href='https://www.zcu.edu.cn/zyjs.htm' target='_blank' title='专业介绍'>专业介绍</a></li><li><a href='http://zf.zcu.edu.cn/jwglxt/xtgl/login_slogin.html' target='_blank' title='教务管理'>教务管理</a></li>";
        }
        searchResult.classList.add("show");
      };
      const searchToggle = () => {
        searchKW === ""
          ? searchClear.classList.remove("show")
          : searchClear.classList.add("show");
        searchKW.trim() === ""
          ? searchBtn.classList.add("disabled")
          : searchBtn.classList.remove("disabled");
        searchKW.trim() === ""
          ? searchResult.classList.remove("show")
          : searchResultToggle();
      };

      searchKey.onfocus = () => {
        nvmSearch.classList.add("active");
      };

      document.addEventListener("click", (e) => {
        if (!nvmSearch.contains(e.target) && searchKW.trim() === "") {
          nvmSearch.classList.remove("active");
        }
      });

      const resetSearch = () => {
        searchKW = "";
        searchKey.value = "";
        searchToggle();
      };

      searchClear.onclick = () => {
        resetSearch();
      };

      searchKey.oninput = (e) => {
        searchKW = e.target.value;
        searchToggle();
      };

      const startSearch = (e) => {
        var url = __core.getOptions.search.url;
        var query = __core.getOptions.search.query;


        if (e) {
          query['keyWord'] = e;
          if (__core.getOptions.search.encode) {
            var query = window.btoa(
              unescape(encodeURIComponent(JSON.stringify(query)))
            );
          }
          window.open(url + "?query=" + query);
        } else {
          alert("请填写搜索内容");
        }

        // window.open(
        //     `https://www.zcu.edu.cn/s?sids=1&kp=1&wd=${e}`,
        //     '_self',
        // ),
        resetSearch();
      };

      searchKey.onkeyup = (e) => {
        if (e.key === "Enter") {
          if (searchKW === "") {
            return;
          } else {
            startSearch(searchKW);
          }
        }
      };

      searchBtn.onclick = () => {
        if (searchBtn.classList.contains("disabled")) {
          nvmSearch.classList.add("active");
        } else {
          startSearch(searchKW);
        }
      };

      // var searchForm = document.querySelector('.top-search-form');

      // if (!searchForm) {
      //     return true;
      // }

      // searchForm.closest('.header-row')?.classList.add('top-search-parent');

      // var topSearchParent = document.querySelector('.top-search-parent'),
      //     timeout;

      // selector[0].onclick = function (e) {
      //     e.stopPropagation();
      //     e.preventDefault();

      //     clearTimeout(timeout);

      //     __core.getVars.elBody.classList.toggle('top-search-open');

      //     __core.getVars.recalls.menureset();

      //     if (__core.getVars.elBody.classList.contains('top-search-open')) {
      //         topSearchParent?.classList.add('position-relative');
      //     } else {
      //         timeout = setTimeout(function () {
      //             topSearchParent?.classList.remove('position-relative');
      //         }, 500);
      //     }

      //     __core.getVars.elBody.classList.remove("primary-menu-open");
      //     __core.getVars.elPageMenu && __core.getVars.elPageMenu.classList.remove('page-menu-open');

      //     if (__core.getVars.elBody.classList.contains('top-search-open')) {
      //         searchForm.querySelector('input').focus();
      //     }
      // };

      // document.addEventListener('click', function (e) {
      //     if (!e.target.closest('.top-search-form')) {
      //         __core.getVars.elBody.classList.remove('top-search-open');
      //         timeout = setTimeout(function () {
      //             topSearchParent?.classList.remove('position-relative');
      //         }, 500);
      //     }
      // }, false);
    },
  };
})();
